package com.hardware.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hardware.common.R;
import com.hardware.entity.Supplier;
import com.hardware.entity.system.Module;
import com.hardware.service.system.ModuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.hardware.utils.RedisConstants.CACHE_SUPPLIER_KEY;

@Slf4j
@RestController
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    private ModuleService moduleService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
//    @Cacheable(value = CACHE_CATEGORY_SHOP_KEY,key = "#page +'_'+ #pageSize")
    public R<Page> page(int page, int pageSize){

        return moduleService.page(page,pageSize);
    }

    /**
     * 根据id删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
//    @CacheEvict(value = CACHE_CATEGORY_SHOP_KEY, allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){

        moduleService.delete(ids);

        return R.success("分类信息删除成功");
    }

    /**
     * 修改供应商状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
//    @CacheEvict(value = CACHE_SUPPLIER_KEY,allEntries = true)
    public R<String> updateByid(@PathVariable Integer status, @RequestParam List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<Module> lqw = new LambdaQueryWrapper<>();
        lqw.in(Module::getId,ids);
        List<Module> collect = ids.stream().map((item) -> {
            Module module = new Module();
            module.setState(Long.valueOf(status));
            module.setId(item);
            return module;
        }).collect(Collectors.toList());
        moduleService.updateBatchById(collect);
        return R.success("更新成功");
    }

    /**
     * 根据条件查询
     * @return
     */
    @GetMapping("/list")
//    @Cacheable(value = "cache:supplier:list")
    public R<List<Module>> list(){
        //条件构造器
        LambdaQueryWrapper<Module> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByDesc(Module::getOrderNo);
        queryWrapper.groupBy(Module::getParentName);

        List<Module> list = moduleService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
//    @Cacheable(value = CACHE_SUPPLIER_KEY,key = "#id")
    public R<Module> get(@PathVariable Long id){
        Module module = moduleService.getById(id);
        return R.success(module);
    }

    /**
     * 新增
     * @param module
     * @return
     */
    @PostMapping
//    @CacheEvict(value = CACHE_SUPPLIER_KEY,allEntries = true)
    public R<String> save(@RequestBody Module module){
        moduleService.save(module);
        return R.success("新增成功");
    }

    /**
     * 修改
     * @param module
     * @return
     */
    @PutMapping
    @CacheEvict(value = CACHE_SUPPLIER_KEY,allEntries = true)
    public R<String> update(@RequestBody Module module){

        moduleService.updateById(module);

        return R.success("修改成功");
    }

}
